home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / qbtree55.zip / XBTREE2.BAS < prev    next >
BASIC Source File  |  1991-07-31  |  14KB  |  410 lines

  1. DECLARE SUB MoveIn (rec$, vseg%, voff%)
  2. DECLARE FUNCTION MoveOut$ (vseg%, voff%, bytes%)
  3. DECLARE FUNCTION Prepare$ (vseg%, voff%, bytes%)
  4. DECLARE FUNCTION OpenXAppFiles% ()
  5. DECLARE FUNCTION BuildXAppFiles% ()
  6. DECLARE FUNCTION ShowXAppFiles% ()
  7. DECLARE FUNCTION CreateXAppFiles% ()
  8.  
  9. DEFINT A-Z
  10. REM $INCLUDE: 'qbtree.bi'
  11.  
  12. 'XBTREE2.BAS - an example application that exercises QBTree.
  13. '(C)1991 Cornel Huth
  14. '31-Jul-1991
  15. '
  16. '------------------------------- DESCRIPTION --------------------------------
  17. '
  18. ' A) Primary key in EMP.DAT is EMP#. For each employee there is one and only
  19. ' one EMP# and for each EMP# there is one and only one employee. Each
  20. ' employee is assigned to a department. EMP.DAT:DEPT#, the foreign key,
  21. ' contains the department number he is assigned to.
  22. '
  23. ' Given an employee number (in EMP.DAT) you can find which department he is
  24. ' assigned. You can also find the name of his manager.
  25. '
  26. '
  27. ' B) Primary key in DEP.DAT is DEPT#. For each department there is one and
  28. ' only one DEPT# and for each DEPT# there is one and only one department.
  29. ' DEP.DAT:MGR#, the foreign key, contains the employee number of that
  30. ' department's manager.
  31. '
  32. ' Given a department number (in DEP.DAT) you can find the name of the
  33. ' manager of that department.
  34. '
  35. '
  36. ' C) Primary key in DEPEMP.DAT is DEP#+EMP#. Since each DEP# is unique and
  37. ' each EMP# is unique, combining the two you get a unique key. The DEP#
  38. ' portion of the key groups all EMP#'s in DEP# together allowing you to get
  39. ' all EMP#'s in a particular DEP#.
  40. '
  41. ' Given a department number (in DEPEMP.DAT) you can find all employees in
  42. ' that department. To get a unique primary key, the employee number is
  43. ' combined with the department number. With QBTREE you can specify a
  44. ' partial key (in this case just DEPT# with an EMP# of 0) and QBTREE will
  45. ' return the first DEPT#+EMP#. Using GetNext() you continue processing
  46. ' this until the DEPT# portion changes.
  47. '
  48. '
  49. '  ====  Primary key (field used to index this file)
  50. '                                            
  51. '  ----  Foreign key (field used to connect to another file's primary key)
  52. '                                           
  53. '  ≡≡≡≡  Used as both primary and foreign key
  54. '
  55. '                                               C) DEPEMP.DAT RECORD*
  56. '                                                  ┌───────┬──────┐
  57. '   ┌────────────────────────────────────┐         │ DEPT# │ EMP# │
  58. '   │                                    │         │ ===== │ ≡≡≡≡ │
  59. '     A) EMP.DAT RECORD                 │         └───────┴──────┘
  60. '┌──────┬───────────────┬────────┬────┐  └────────────────────┘
  61. '│ EMP# │ EMPLOYEE NAME │ DEPT#  │ WG │
  62. '│ ==== │               │ -----  │    │     B) DEP.DAT RECORD
  63. '└──────┴───────────────┴────────┴────┘   ┌───────┬───────────┬──────┐
  64. '                          │             │ DEPT# │ DEPT NAME │ MGR# │
  65. '   │                       └─────────── │ ====  │           │ ---- │
  66. '   │                                     └───────┴───────────┴──────┘
  67. '   │                                                            │
  68. '   └────────────────────────────────────────────────────────────┘
  69. '
  70. ' Example datafile contents:
  71. '
  72. '            EMP.DAT                  DEP.DAT        DEPEMP.DAT*
  73. '  EMP# EMPLOYEE NAME   D# WG    D# DEPT NAME  MGR#    D# EMP#
  74. '  ---- --------------- -- --    -- ---------- ----    -- ----
  75. '  1001 Frank Haas      12 15    10 Purchasing 1002    10 1002
  76. '  1002 Wendy Gibson    10 15    11 Accounting 2173    11 2173
  77. '  1125 Willie McAffee  14  9    12 Legal      1001    12 1001
  78. '  1507 David Robinson  13  9    13 MIS        1507    13 1507
  79. '  2173 Jackie Stewart  11 17    14 Personnel  1125    14 1125
  80. '  ... and so on                 ... and so on         ... and so on
  81. '
  82. ' * DEPEMP.DAT carries no information other than the DEP# in DEP.DAT
  83. ' and the EMP# in EMP.DAT. This means we do not need to carry a data
  84. ' file for the DEPEMP information. What is listed in this description
  85. ' as DEPEMP.DAT will actually be the index file itself (DEPEMP.NDX).
  86. '----------------------------------------------------------------------------
  87. '
  88. ' This program will output to the screen two logical tables. Table 1, the
  89. ' BY EMPLOYEE table, will have the employee's number, name, wage grade,
  90. ' department, and manager. Table 2, the BY DEPARTMENT table, will have a list of
  91. ' employees in each department.
  92. '
  93. '============================================================================
  94.  
  95. ' QBTREE file number equates
  96. CONST EMPdf = 0         'EMP.DAT QBTREE data file number
  97. CONST DEPdf = 1         'DEP.DAT
  98. CONST MDF = 1           'max data files needed (last data file number)
  99.  
  100. CONST EMPif = 0         'EMP.NDX QBTREE index file number
  101. CONST DEPif = 1         'DEP.NDX
  102. CONST DEPEMPif = 2      'DEPEMP.NDX
  103. CONST MKF = 2           'max key files needed (last key file number)
  104.  
  105. CONST ASMODE = 2        'files opened in compatiblity mode
  106.  
  107. ' Employee data record type
  108. TYPE EmpDataTYPE
  109. Number AS STRING * 4
  110. zName AS STRING * 15
  111. DeptNo AS STRING * 2
  112. WG AS INTEGER
  113. END TYPE '23
  114. DIM SHARED EMP AS EmpDataTYPE
  115.  
  116. ' Department data record type
  117. TYPE DepDataTYPE
  118. Number AS STRING * 2
  119. zName AS STRING * 10
  120. MgrNo AS STRING * 4
  121. END TYPE '16
  122. DIM SHARED DEP AS DepDataTYPE
  123.  
  124. 'size FixedStr to largest TYPE structure used in QBTREE access
  125. DIM SHARED FixedStr AS STRING * 23
  126.  
  127. DIM SHARED XEmpData$
  128. DIM SHARED XDepData$
  129. DIM SHARED XEmpIndex$
  130. DIM SHARED XDepIndex$
  131. DIM SHARED XDepEmpIndex$
  132.  
  133. ' We'll create 3 key files and 2 data files using the info from the
  134. ' DATA statements below. Once built we'll show two tables based on the data
  135.  
  136. CLS
  137. stat = InitQBTREE(MKF, MDF)
  138. IF stat = 0 THEN
  139.    stat = CreateXAppFiles
  140.    IF stat = 0 THEN
  141.       stat = OpenXAppFiles
  142.       IF stat = 0 THEN
  143.          stat = BuildXAppFiles
  144.          IF stat = 0 THEN
  145.             stat = ShowXAppFiles
  146.             IF stat THEN
  147.                PRINT "Error"; stat; "from ShowXAppFiles"
  148.             END IF
  149.          ELSE
  150.             PRINT "Error"; stat; "from BuildXAppFiles"
  151.          END IF
  152.       ELSE
  153.          PRINT "Error"; stat; "from OpenXAppFiles"
  154.       END IF
  155.    ELSE
  156.       PRINT "Error"; stat; "from CreateXAppFiles"
  157.    END IF
  158. ELSE
  159.    PRINT "Error"; stat; "from InitQBTREE"
  160. END IF
  161. nul = ExitQBTREE
  162. END
  163.  
  164.  
  165. ' We'll use DATA statements to simplify getting the initial data
  166.  
  167. ' XApp employee data
  168. EmpData:
  169. DATA 11
  170. DATA 1001,Frank Hass,12,15
  171. DATA 1002,Wendy Gibson,10,15
  172. DATA 1125,Willie McAffee,14,9
  173. DATA 1507,David Robinson,13,9
  174. DATA 1173,Jackie Stewart,11,17
  175. DATA 4105,Beatrice South,10,5
  176. DATA 4288,Jim Davies,10,5
  177. DATA 4901,Tom Cassidy,14,4
  178. DATA 3149,Nancy Cannon,13,7
  179. DATA 3510,John Madison,12,12
  180. DATA 3685,Chris Ho,13,9
  181.  
  182. ' XApp department data
  183. DepData:
  184. DATA 5
  185. DATA 10,Purchasing,1002
  186. DATA 11,Accounting,1173
  187. DATA 12,Legal,1001
  188. DATA 13,MIS,1507
  189. DATA 14,Personnel,1125
  190.  
  191. FUNCTION BuildXAppFiles
  192.  
  193. 'using the info in the DATA statements build the XApp files
  194.  
  195. PRINT "Building employee data and index files...";
  196. RESTORE EmpData
  197. READ EmpRecs
  198. FOR i = 1 TO EmpRecs
  199.    READ EMP.Number, EMP.zName, EMP.DeptNo, EMP.WG
  200.    key$ = EMP.Number
  201.    rec$ = MoveOut$(VARSEG(EMP), VARPTR(EMP), LEN(EMP))
  202.    stat = AddKeyRecord(EMPif, EMPdf, key$, rec$)
  203.    IF stat THEN EXIT FOR
  204. NEXT
  205. IF stat = 0 THEN
  206.    PRINT "ok."
  207.    PRINT "Building department data and index files...";
  208.    RESTORE DepData
  209.    READ DepRecs
  210.    FOR i = 1 TO DepRecs
  211.       READ DEP.Number, DEP.zName, DEP.MgrNo
  212.       key$ = DEP.Number
  213.       rec$ = MoveOut$(VARSEG(DEP), VARPTR(DEP), LEN(DEP))
  214.       stat = AddKeyRecord(DEPif, DEPdf, key$, rec$)
  215.       IF stat THEN EXIT FOR
  216.    NEXT
  217.    IF stat = 0 THEN
  218.       PRINT "ok."
  219.       PRINT "Building department+employee index file...";
  220.  
  221.       ' to build this index file we use the employee file just built.
  222.       ' a shortcoming of this is that departments with no employees
  223.       ' (unlikely) assigned will not be represented in the index file.
  224.  
  225.       recno& = 0 'we won't be needing data record pointers for StoreKey()
  226.  
  227.       stat = GetFirst(EMPif, EMPdf, key$, rec$)
  228.       DO WHILE stat = 0
  229.  
  230.          'r